home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreappleevents / filehelpers.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.2 KB  |  84 lines

  1. /*
  2.     File:        FileHelpers.c
  3.  
  4.     Contains:    Functions to help you when working with files.
  5.  
  6.     Written by: Andy Bachorski    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. //    Conditionals to setup the build environment the way we like it.
  25. #include "PrivateConditionals.h"
  26.  
  27.  
  28. //**********    Universal Headers        ****************************************
  29.  
  30. #include <Errors.h>
  31. #include <Files.h>
  32. #include <Resources.h>
  33.  
  34.  
  35. //**********    Project Headers            ****************************************
  36.  
  37. #include "FileHelpers.h"
  38.  
  39.  
  40. //******************************************************************************
  41.  
  42. OSErr OpenResFileForWrite( const FSSpec *fileFSSPtr, SInt16 *resFileRef )
  43. {
  44.     OSErr        anErr = noErr;
  45.     
  46.     *resFileRef = FSpOpenResFile( fileFSSPtr, fsRdWrPerm );
  47.     if ( *resFileRef == -1 )
  48.     {
  49.         anErr = ResError();
  50.     }
  51.     else    //    Got it open, but can it be written to?
  52.     {
  53.         if ( ! IsResourceFileRefNumWritable( *resFileRef ) )
  54.         {
  55.             anErr = permErr;
  56.             CloseResFile( *resFileRef );
  57.             *resFileRef = -1;
  58.         }
  59.     }
  60.     
  61.     return anErr;
  62. }//end OpenResFileForWrite
  63.  
  64. //**************************************************************************
  65.  
  66. Boolean IsResourceFileRefNumWritable( SInt16 refNum )
  67. {
  68.     FCBPBRec    fcbPB;
  69.     Boolean        result = false;
  70.  
  71.     fcbPB.ioNamePtr = nil;
  72.     fcbPB.ioVRefNum = 0;
  73.     fcbPB.ioRefNum = refNum;
  74.     fcbPB.ioFCBIndx = 0;
  75.     if ( PBGetFCBInfoSync( &fcbPB ) == noErr )
  76.     {
  77.         result = ( (fcbPB.ioFCBFlags & (1 << 8)) != 0 );
  78.     }
  79.  
  80.     return result;
  81. }//end IsResourceFileRefNumWritable
  82.  
  83. //******************************************************************************
  84.